  <template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="年份" prop="year">
          <el-input v-model="queryParams.year" placeholder="请输入年份" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="项目名" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入项目名" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="项目状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态">
            <el-option key="1" label="未开始" value="未开始"></el-option>
            <el-option key="2" label="进行中" value="进行中"></el-option>
            <el-option key="3" label="已结束" value="已结束"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['lxy:project:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['lxy:project:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['lxy:project:remove']">删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="年份" align="center" prop="year" />
        <el-table-column label="项目名" align="center" prop="name" />
        <el-table-column label="描述" align="center" prop="content" />
        <el-table-column label="报名开始时间" align="center" prop="startTime" width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="报名截止时间" align="center" prop="endTime" width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="补报开始时间" align="center" prop="reselectStart" width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.reselectStart, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="补报截止时间" align="center" prop="reselectEnd" width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.reselectEnd, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目状态" align="center" prop="status" />
        <el-table-column label="携带家属人数上限" align="center" prop="bringdepeMax" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleRoute(scope.row)"
              v-hasPermi="['lxy:project:edit']">查看路线</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['lxy:project:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['lxy:project:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />

      <!-- 添加或修改项目对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="150px">
          <el-form-item label="年份" prop="year">
            <el-input v-model="form.year" placeholder="请输入年份" />
          </el-form-item>
          <el-form-item label="项目名" prop="name">
            <el-input v-model="form.name" placeholder="请输入项目名" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option key="1" label="未开始" value="未开始"></el-option>
              <el-option key="2" label="进行中" value="进行中"></el-option>
              <el-option key="3" label="已结束" value="已结束"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="描述">
            <editor v-model="form.content" :min-height="192" />
          </el-form-item>
          <el-form-item label="报名开始时间" prop="startTime">
            <el-date-picker clearable v-model="form.startTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择报名开始时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="报名截止时间" prop="endTime">
            <el-date-picker clearable v-model="form.endTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择报名截止时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="补报开始时间" prop="reselectStart">
            <el-date-picker clearable v-model="form.reselectStart" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择补报开始时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="补报截止时间" prop="reselectEnd">
            <el-date-picker clearable v-model="form.reselectEnd" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择补报截止时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="携带家属人数上限" prop="bringdepeMax">
            <el-input v-model="form.bringdepeMax" placeholder="请输入携带家属人数上限" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>

  <script>
  import { listProject, getProject, delProject, addProject, updateProject } from "@/api/lxy/project";

  export default {
    name: "Project",
    data() {
      return {
        // 当前年份
        currentYear:this.getCurrentYear(),
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 项目表格数据
        projectList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          year: undefined,
          name: undefined,
          status: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          id: [
            { required: true, message: "项目表不能为空", trigger: "blur" }
          ],
          year: [
            { required: true, message: "年份不能为空", trigger: "blur" }
          ],
          name: [
            { required: true, message: "项目名不能为空", trigger: "blur" }
          ],
          content: [
            { required: false, message: "描述能为空", trigger: "blur" }
          ],
          startTime: [
            { required: true, message: "报名开始时间不能为空", trigger: "blur" }
          ],
          endTime: [
            { required: true, message: "报名截止时间不能为空", trigger: "blur" }
          ],
          reselectStart: [
            { required: false, message: "补报开始时间能为空", trigger: "blur" }
          ],
          reselectEnd: [
            { required: false, message: "补报截止时间能为空", trigger: "blur" }
          ],
          bringdepeMax: [
            { required: true, message: "携带家属人数上限不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询项目列表 */
      getList() {
        this.loading = true;
        listProject(this.queryParams).then(response => {
          this.projectList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          year: undefined,
          name: undefined,
          content: undefined,
          startTime: undefined,
          endTime: undefined,
          reselectStart: undefined,
          reselectEnd: undefined,
          status: undefined,
          bringdepeMax: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          delFlag: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加项目";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids
        getProject(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改项目";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            // 检查输入的年份是否为当前年份
            if (this.form.year !== this.currentYear) {
              // 构建确认框消息，仅年份部分加红色
              const confirmationMessage = `
            您输入的年份<span style="color: red;">${this.form.year}</span>，当前年份为<span style="color: red;">${this.currentYear}</span>，是否确认继续提交？`;

              // 显示确认对话框
              this.$confirm(confirmationMessage, "年份确认", {
                confirmButtonText: "继续提交",
                cancelButtonText: "取消",
                type: "warning",
                dangerouslyUseHTMLString: true
              })
                .then(() => {
                  // 继续提交逻辑
                  this.submitFormLogic();
                })
                .catch(() => {
                  // 如果用户取消，什么都不做
                });
            } else {
              // 如果年份是当前年份，继续提交逻辑
              this.submitFormLogic();
            }
          }
        });
      },
      /** 提交按钮的逻辑处理 */
      submitFormLogic() {
        this.buttonLoading = true;
        if (this.form.id != null) {
          updateProject(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          });
        } else {
          addProject(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除项目编号为"' + ids + '"的数据项？').then(() => {
          this.loading = true;
          return delProject(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        }).finally(() => {
          this.loading = false;
        });
      },
      /** 获取当前年份 */
      getCurrentYear() {
        const currentDate = new Date();
        return currentDate.getFullYear();
      },

      handleRoute: function (row) {
        const id = row.id;
        this.$router.push({
          path: '/lxy/route/show/' + id,
          query: {
            projectId: row.id,
            projectName: row.name
          }
        })
      },
    }
  };
  </script>
